<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:12
 */
namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 技师阶梯提成配置附表
 * @package app\admin\model
 */
class EngineerConfigItem extends Model
{
    protected $name = 'qt_engineer_config_item';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    public static function setData($getData,$tempId,$userInfo){
        $cal_project = json_decode($getData['cal_project'],true);//计算上钟项目
        $clock_type = explode(',',$getData['clock_type']);//计算上钟类型
        $com_project = json_decode($getData['com_project'],true);//提成项目
        $com_type = explode(',',$getData['com_type']);//提成上钟类型
        $result = [];
        $project = [];
        foreach ($cal_project as $key=>$value){
            $project[$key] = $value['project_id'].','.$value['project_number'];
        }
        $comProjectData = [];
        foreach ($com_project as $key=>$value){
            $comProjectData[$key] = $value['project_id'].','.$value['project_number'];
        }
        $arr = [$project,$clock_type,$comProjectData,$com_type];
        $data = self::cartesian($arr);
        foreach ($data as $key=>$value){
            $valueData = explode(',',$value);
            //17,1,1,17,1,1
            $result[$key] = [
                'store_id'=>$userInfo['store_id'],
                'user_id'=>$userInfo['id'],
                'config_id'=>$tempId,
                'data'=>$value,
                'project_id'=>$valueData[0],
                'project_number'=>$valueData[1],
                'clock_type'=>$valueData[2],
                'clock_min'=>$getData['clock_min'],
                'clock_max'=>$getData['clock_max'],
                'com_project_id'=>$valueData[3],
                'com_project_number'=>$valueData[4],
                'com_clock_type'=>$valueData[5],
                'money'=>$getData['money'],
                'money_type'=>$getData['money_type'],
                'create_time'=>time(),
            ];
        }
        if(Db::name('qt_engineer_config_item')->insertAll($result)){
            return true;
        }else{
            return false;
        }
    }


    public static function cartesian($arr,$str = array()){
        //去除第一个元素
        $first = array_shift($arr);
        //判断是否是第一次进行拼接
        if(count($str) > 1) {
            foreach ($str as $k => $val) {
                foreach ($first as $key => $value) {
                    //最终实现的格式 1,3,76
                    //可根据具体需求进行变更
                    $str2[] = $val.','.$value;
                }
            }
        }else{
            foreach ($first as $key => $value) {
                //最终实现的格式 1,3,76
                //可根据具体需求进行变更
                $str2[] = $value;
            }
        }
        //递归进行拼接
        if(count($arr) > 0){
            $str2 = self::cartesian($arr,$str2);
        }
        //返回最终笛卡尔积
        return $str2;
    }




}
